//获取权限方法
var userInfo = getAuthority();
var powerlist = JSON.parse(userInfo).permissions;
/*自定义函数判断权限*/
template.defaults.imports.hasPower = function(data){
    if(powerlist.includes(data)){
        return true;
    }
    return false;
};

layui.use(['admin','element', 'layer','form','notice'], function(){
    var admin = layui.admin;
    var element = layui.element;
    var layer = layui.layer;
    var form = layui.form;
    var notice = layui.notice;
    
    var report_type=""; //保存报告期类型
    var rcid = '';//区划配置版本
    var row_info="",column_info= "",row_type= "",column_type="";
    var x="", y= "";
    var curTarget= {
        node: [],
        type: ""
    };
    var isCreate= false;
    var curMeta= admin.getLayerData();  // 获取列表页面传递的数据; 保存综合对象信息及id
    var dataTemp={//模板
        id: undefined,
        version: undefined
    };
    var matid= ""; //保存综合对象id
    var data_format= ""; //保存数据类型 0-总量数据 1-分地区数据
    var showRegionItem=false;
    var newtemp = false;//是否新模板
    var tableWidth = 'auto',tableHeight = "";
    var filter= {
        period: undefined,
        region: undefined,
        curRegion: {}//区划
    };

    var setting={//树设置
        regionSetting:{//区划树设置
            async: {
                enable: true,
                url: '',
                type: "get",
            },
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false,
                drag: {
                    isCopy: true,
                    isMove: false,
                    prev: false,
                    next: false,
                    inner: false
                }
            },
            data: {
                key: {
                    children: "sub"
                },
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onClick: regionTreeOnClick,
                onExpand: onExpandRegionTree
            },
            view: {
                showIcon: true,
                showLine: true
            }
        },
        xysetting: {
            //主栏、宾栏设置
            edit: {
                enable: true,
                showRemoveBtn: true,
                showRenameBtn: false,
                drag: {
                    // prev: prevTree,
                    // next: nextTree,
                    // inner: innerTree
                }
            },
            data: {
                key: {
                    children: "sub"
                },
                simpleData: {
                    enable: true
                }
            },
            callback: {
                // onRemove: onRemove,
                // onMouseUp: onMouseUp
            },
            view: {
                showIcon: true,
                showLine: true
            }
        }
    }

    if(userInfo){
        var access_token = localStorage.getExpire("access_token");
        var token_type = localStorage.getExpire("token_type");
        Authorization = token_type+" "+access_token;

        getImportDataPower();
        init();
    }

    form.on('select(regionSet)', function(data){
        queryRegionTree(data.value);
    });

    $("#regionInput").click(function() {
        $("#groupRegion").show();
    });

    $("#uploadDataBtn").click(function () {
        showUploadDataModel();
    });

    $("#inputDataBtn").click(function(){
        handleUploadData();
    });

    function getImportDataPower() {
        var str = template("templateManage-tmpl",{});
        document.getElementById("templateManage-tmpl-box").innerHTML = str;//模板
    }
    function init() {
        getMacroTemplate(function(arr){
            renderTemplate(arr);
        });
        createReportDataTree();
        getRegionVersion();
    }

    // 通过综合对象id查询模板
    function getMacroTemplate(callback){
        axios.get(macroUrl.template.queryTemplate+curMeta.id,{
            headers:{
                'Authorization':Authorization
            }
        })
            .then(function (response) {
                if(response.status == 200 &&response.data.message == "success"){                    
                    if(response.data.data!=null){
                        var arr = response.data.data;                        
                        callback(arr);
                    }
                }
            })
            .catch(function (error) {
                console.log(error);
                if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                    // 进行重新登陆
                    localStorage.clear();
                    top.location.href=loginPageUrl;
                }
            });
    }

    function renderTemplate(arr){
        var datas = strAllReplaceLine(arr);
        column_info = JSON.parse(datas.column_info);
        row_info = JSON.parse(datas.row_info);
        row_type = datas.row_type == 1 ? "iden" : "group";
        column_type = datas.column_type == 1 ? "iden" : "group";
        dataTemp = {
            id: datas.id,
            version: datas.version
        };

        $.fn.zTree.init($("#x"), setting.xysetting, row_info);
        $.fn.zTree.init($("#y"), setting.xysetting, column_info);
        $("#left_div1").html(datas.cross_table);
        $("#right_divx").html(datas.column_table);
        $("#left_div2").html(datas.row_table);
        $("#right_div2").html(datas.data_table);
        tableSize();
        x = row_type;
        y = column_type;
        setTableStyle();
        main.setXyInfo(row_type, column_type);
        bindValue();
    }

    function createReportDataTree(){
        //初始构建报告期树
        var setting = {
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false,
                drag: {
                    isCopy: false,
                    isMove: false,
                    prev: false,
                    next: false,
                    inner: false
                }
            },
            data: {
                key: {
                    children: "sub"
                },
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onCheck: getCheckedNodes
            },
            view: {
                showIcon: false,
                showLine: false
            },
            check: {
                enable: true,
                nocheckInherit: false, //设置子节点是否自动继承 nocheck = true 。
                chkboxType: { Y: "ps", N: "ps" }
            }
        };

        var zNodes = [];
        var date = new Date();
        var startYear = 1949;
        var endYear = date.getFullYear();
        var len = endYear - startYear + 1;
        
        switch (Number(curMeta.reportType)) {
            case 1:
                zNodes = [
                    {
                        id: "y_1",
                        year: 9999,
                        month: 99,
                        period_type: "dir",
                        Id: 0,
                        name: "年",
                        attribute: {
                            isRegionCode: false,
                            groupCode: "9999-99",
                            groupName: "年"
                        },
                        cat_id: "y_1",
                        nocheck: true
                    }
                ];
                for (var i = len; i > 0; i--) {
                    var node = {
                        id: "y_" + (i + 2),
                        year: startYear + i,
                        month: 0,
                        // period_type:"value",
                        period_type: "dir",
                        pId: "y_1",
                        name: startYear + i + "年",
                        attribute: {
                            isRegionCode: false,
                            groupCode: startYear + i,
                            period: startYear + i,
                            groupName: startYear + i + "年",
                            ig_type: "group",
                            path: "",
                            isPeriod: true
                        },
                        cat_id: "y_" + (i + 2),
                        sub: []
                    };
                    zNodes.push(node);
                }
                break;
            case 2:
                zNodes = [
                    {
                        id: "m_1",
                        year: 9999,
                        month: 99,
                        period_type: "dir",
                        Id: 0,
                        name: "月",
                        attribute: {
                            isRegionCode: false,
                            groupCode: "9999-99",
                            groupName: "月"
                        },
                        cat_id: "m_1",
                        nocheck: true
                    }
                ];
                for (var i = len; i > 0; i--) {
                    var node = {
                        id: "m_" + (i + 2),
                        year: startYear + i,
                        month: 0,
                        period_type: "dir",
                        pId: "m_1",
                        name: startYear + i + "年",
                        attribute: {
                            isRegionCode: false,
                            groupCode: startYear + i,
                            period: startYear + i,
                            groupName: startYear + i + "年",
                            ig_type: "group",
                            path: "",
                            isPeriod: true
                        },
                        cat_id: "m_" + (i + 2),
                        sub: []
                    };
                    zNodes.push(node);
                    for (var j = 0; j < 12; j++) {
                        var node = {
                            id: "m_" + (i + 2) + "_" + (j + 1),
                            year: startYear + i,
                            month: 0,
                            period_type: "value",
                            pId: "m_" + (i + 2),
                            name: startYear + i + "年" + (j + 1) + "月",
                            attribute: {
                                isRegionCode: false,
                                groupCode: startYear + i + "-" + (j + 1),
                                period: startYear + i + "-" + (j + 1),
                                groupName: startYear + i + "年" + (j + 1) + "月",
                                ig_type: "group",
                                path: "",
                                isPeriod: true
                            },
                            cat_id: "m_" + (i + 2) + "_" + (j + 1),
                            sub: []
                        };
                        zNodes.push(node);
                    }
                }
                break;
            case 32:
                zNodes = [
                    {
                        id: "q_2_1",
                        year: 9999,
                        month: 99,
                        period_type: "dir",
                        Id: 0,
                        name: "季2",
                        attribute: {
                            isRegionCode: false,
                            groupCode: "9999-99",
                            groupName: "季2"
                        },
                        cat_id: "q_2_1",
                        nocheck: true
                    }
                ];
                for (var i = len; i > 0; i--) {
                    var node = {
                        id: "q_2_" + (i + 2),
                        year: startYear + i,
                        month: 0,
                        period_type: "dir",
                        pId: "q_2_1",
                        name: startYear + i + "年",
                        attribute: {
                            isRegionCode: false,
                            groupCode: startYear + i,
                            period: startYear + i,
                            groupName: startYear + i + "年",
                            ig_type: "group",
                            path: "",
                            isPeriod: true
                        },
                        cat_id: "q_2_" + (i + 2),
                        sub: []
                    };
                    zNodes.push(node);
                    var qm = [2, 5, 8, 11];
                    for (var j = 0; j < 4; j++) {
                        var node = {
                            id: "q_2_" + (i + 2) + "_" + qm[j],
                            year: startYear + i,
                            month: 0,
                            period_type: "value",
                            pId: "q_2_" + (i + 2),
                            name: startYear + i + "年" + (j + 1) + "季",
                            attribute: {
                                isRegionCode: false,
                                groupCode: startYear + i + "-" + qm[j],
                                period: startYear + i + "-" + qm[j],
                                groupName: startYear + i + "年" + qm[j] + "季",
                                ig_type: "group",
                                path: "",
                                isPeriod: true
                            },
                            cat_id: "q_2_" + (i + 2) + "_" + qm[j],
                            sub: []
                        };
                        zNodes.push(node);
                    }
                }
                break;
            case 33:
                zNodes = [
                    {
                        id: "q_3_1",
                        year: 9999,
                        month: 99,
                        period_type: "dir",
                        Id: 0,
                        name: "季3",
                        attribute: {
                            isRegionCode: false,
                            groupCode: "9999-99",
                            groupName: "季3"
                        },
                        cat_id: "q_3_1",
                        nocheck: true
                    }
                ];
                for (var i = len; i > 0; i--) {
                    var node = {
                        id: "q_3_" + (i + 2),
                        year: startYear + i,
                        month: 0,
                        period_type: "dir",
                        pId: "q_3_1",
                        name: startYear + i + "年",
                        attribute: {
                            isRegionCode: false,
                            groupCode: startYear + i,
                            period: startYear + i,
                            groupName: startYear + i + "年",
                            ig_type: "group",
                            path: "",
                            isPeriod: true
                        },
                        cat_id: "q_3_" + (i + 2),
                        sub: []
                    };
                    zNodes.push(node);
                    var qm = [3, 6, 9, 12];
                    for (var j = 0; j < 4; j++) {
                        var node = {
                            id: "q_3_" + (i + 2) + qm[j],
                            year: startYear + i,
                            month: 0,
                            period_type: "value",
                            pId: "q_3_" + (i + 2),
                            name: startYear + i + "年" + (j + 1) + "季",
                            attribute: {
                                isRegionCode: false,
                                groupCode: startYear + i + "-" + qm[j],
                                period: startYear + i + "-" + qm[j],
                                groupName: startYear + i + "年" + qm[j] + "季",
                                ig_type: "group",
                                path: "",
                                isPeriod: true
                            },
                            cat_id: "q_3_" + (i + 2) + qm[j],
                            sub: []
                        };
                        zNodes.push(node);
                    }
                }
                break;
            case 4:
                zNodes = [
                    {
                        id: "h_1",
                        year: 9999,
                        month: 99,
                        period_type: "dir",
                        Id: 0,
                        name: "半年",
                        attribute: {
                            isRegionCode: false,
                            groupCode: "9999-99",
                            groupName: "半年"
                        },
                        cat_id: "h_1",
                        nocheck: true
                    }
                ];
                for (var i = len; i > 0; i--) {
                    var node = {
                        id: "h_" + (i + 2),
                        year: startYear + i,
                        month: 0,
                        period_type: "dir",
                        pId: "h_1",
                        name: startYear + i + "年",
                        attribute: {
                            isRegionCode: false,
                            groupCode: startYear + i,
                            period: startYear + i,
                            groupName: startYear + i + "年",
                            ig_type: "group",
                            path: "",
                            isPeriod: true
                        },
                        cat_id: "h_" + (i + 2),
                        sub: []
                    };
                    zNodes.push(node);
                    for (var j = 0; j < 2; j++) {
                        var node = {
                            id: "h_" + (i + 2) + "_" + (j + 1),
                            year: startYear + i,
                            month: 0,
                            period_type: "value",
                            pId: "h_" + (i + 2),
                            name:
                            startYear + i + "年" + (j + 1 == 1 ? "上" : "下") + "半年",
                            attribute: {
                                isRegionCode: false,
                                groupCode: startYear + i + "-" + (j + 1),
                                period: startYear + i + "-" + (j + 1),
                                groupName: startYear + i + "年" + (j + 1) + "半年",
                                ig_type: "group",
                                path: "",
                                isPeriod: true
                            },
                            cat_id: "h_" + (i + 2) + "_" + (j + 1),
                            sub: []
                        };
                        zNodes.push(node);
                    }
                }
                break;
            default:
                zNodes = [
                    {
                        id: "y_1",
                        year: 9999,
                        month: 99,
                        period_type: "dir",
                        Id: 0,
                        name: "年",
                        attribute: {
                            isRegionCode: false,
                            groupCode: "9999-99",
                            groupName: "年"
                        },
                        cat_id: "y_1",
                        nocheck: true
                    }
                ];
                for (var i = len; i > 0; i--) {
                    var node = {
                        id: "y_" + (i + 2),
                        year: startYear + i,
                        month: 0,
                        // period_type:"value",
                        period_type: "dir",
                        pId: "y_1",
                        name: startYear + i + "年",
                        attribute: {
                            isRegionCode: false,
                            groupCode: startYear + i,
                            period: startYear + i,
                            groupName: startYear + i + "年",
                            ig_type: "group",
                            path: "",
                            isPeriod: true
                        },
                        cat_id: "y_" + (i + 2),
                        sub: []
                    };
                    zNodes.push(node);
                }
        }
        $.fn.zTree.init($("#reportData"), setting, zNodes);
    }

    function getCheckedNodes() {
        //获取选中的报告期
        var zTree = $.fn.zTree.getZTreeObj("reportData");
        var checkedNodes = zTree.getNodesByFilter(treeFilter); // 查找节点集合
        var checkstring = JSON.stringify(checkedNodes);
        var checkobj = JSON.parse(checkstring);
        filter.period = checkobj;
        buildTables(0, checkobj);
    }

    //查询区划配置版本
    function getRegionVersion() {
        let req = {
            current:1,
            flag:0,
            size:pageSize_noPage
        }
        axios.get(regionUrl.catalog.catalogList,{
            params:req,
            headers:{
                'Authorization':Authorization
            }
        })
            .then(function (response) {
                if(response.status == 200 &&response.data.message == "success"){
                    var arr = response.data.data;
                    var str = template("region-tree-tmpl",arr);
                    document.getElementById("region-tree-box").innerHTML = str;
                    form.render('select')
                    if(arr.total>0){
                        rcid = arr.records[0].id;
                        lazyLoadRegionTree();
                    }
                }
            })
            .catch(function (error) {
                console.log(error);
                if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                    // 进行重新登陆
                    localStorage.clear();
                    top.location.href=loginPageUrl;
                }
            });
    }

    //查询行政区划树
    function queryRegionTree(value) {
        rcid = value;
        lazyLoadRegionTree();
    }

    //区划树放在下方懒加载
    function lazyLoadRegionTree(curRegionCode) {
        //查询区划放在下方显示的树
        var code = "";
        if (curRegionCode) {
            code = curRegionCode;
        }
        var req = {
            rcId: rcid,
            regionCode: code,
            regionCodeType: 1
        };
        getRegion(req,function (datas) {
            $.fn.zTree.init($("#groupRegion"), setting.regionSetting, datas);
        })
    }

    //查询区划数据
    function getRegion(req,callback) {
        axios.get(regionUrl.info.queryRegion,{
            params: req,
            headers:{
                'Authorization':Authorization
            }
        })
            .then(function (response) {
                if(response.status == 200 &&response.data.message == "success"){
                    var arr = response.data.data;
                    var datas = [];
                    for(var i = 0; i < arr.length; i++ ){
                        datas[i] = strAllReplaceLine(arr[i]);
                    }
                    if (datas.length > 0) {
                        for (const row of datas) {
                            row.cat_id = row.id;
                            row.isParent = true;
                            row.attribute = {
                                isRegionCode: true,
                                groupCode: row.code,
                                groupName: row.name,
                                isPeriod: false,
                                period: "",
                                ig_type: "group",
                                path: "",
                                pathName: "",
                                idencomparsion: ""
                            };
                            row.sub = []; //存放子节点
                            row.drag = true;
                        }
                    }
                    callback(datas);
                }
            })
            .catch(function (error) {
                console.log(error);
                if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                    // 进行重新登陆
                    localStorage.clear();
                    top.location.href=loginPageUrl;
                }
            });
    }

    //查询下方区划树的下级区划值（懒加载）
    function onExpandRegionTree(event, treeId, treeNode) {
        if(treeNode.sub.length<1){
            var req = {
                rcId: rcid,
                regionCode: treeNode.code,
                regionCodeType: 1
            };
            getRegion(req,function (datas) {
                const treeObj = $.fn.zTree.getZTreeObj("groupRegion");
                treeObj.addNodes(treeNode, datas);
            });
        }

    }

    //区划树点击
    function regionTreeOnClick(event, treeId, treeNode){
        $("#regionInput input").val(treeNode.name);
        filter.curRegion.regioncode  = treeNode.code;
        $("#groupRegion").hide();
    }

    function tableSize() {
        const height = document.documentElement.clientHeight - 43 * 4 - 130;
        tableWidth = $("#left_div2").width() + $("#right_div2").width()+2;
        // tableHeight = height - $("#left_div1").height();
        $(".tableWidth").css("width",tableWidth)
    }

    //设置表格滑动样式
    function setTableStyle() {
        var right_div2 = document.getElementById("right_div2");
        right_div2.onscroll = function() {
            var right_div2_top = scrollTop;
            var right_div2_left = scrollLeft;
            document.getElementById("left_div2").scrollTop = right_div2_top;
            document.getElementById("right_div1").scrollLeft = right_div2_left;
        };
        //设置右边div宽度
        tableSize();
    }

    function bindValue() {
        let input = $("#data_table").find("input");
        $("#data_table")
            .find("input")
            .blur(function() {
                $(this)
                    .siblings("span")
                    .html($(this).val());
            });
        input.bind({
            //paste事件
            paste: function(e) {
                var eve = e.originalEvent;
                var cp = eve.clipboardData;
                var str = null;
                // for IE
                var clipboardData = window.clipboardData;
                // for chrome
                if (!clipboardData) {
                    clipboardData = e.originalEvent.clipboardData;
                }
                str = clipboardData.getData("Text");

                var len = str.split("\n");
                var trStr;
                var columIndex = $(this)
                    .parents("td")
                    .index();
                var rowIndex = $(this)
                    .parents("tr")
                    .index();
                //从excle表格中复制的数据，最后一行为空行，因此无需对len数组中最后的元素进行处理
                for (var i = 0; i < len.length - 1; i++) {
                    //按制表符分割当前行，获取单元格数据
                    trStr = len[i].split(/\t+/);
                    var text = "";
                    for (var j = 0; j <= trStr.length - 1; j++) {
                        text += trStr[j] + "-";
                        //填充数据到table
                        var colum = parseInt(columIndex) + j;
                        var td = $("#data_table").find(
                            'td[rowidx="' +
                            (parseInt(rowIndex) + i) +
                            '"][colidx="' +
                            colum +
                            '"]'
                        );
                        if (td.length == 0) {
                            td = $("#data_table").find(
                                'td[rindex="' +
                                (parseInt(rowIndex) + i) +
                                '"][cindex="' +
                                colum +
                                '"]'
                            );
                        }
                        //var td = $("#data_table").find('td[rowidx="' + (parseInt(rowIndex)+i) + '"][colidx="' + colum + '"]');
                        //var td = $("#data_table").find('td[rindex="' + (parseInt(rowIndex)+i) + '"][cindex="' + colum + '"]');
                        /*var td = $("#data_table").find('td[rindex="' + (parseInt(rowIndex)+i) + '"][colidx="' + colum + '"]');
                          var td = $("#data_table").find('td[rindex="' + (parseInt(rowIndex)+i) + '"][cindex="' + colum + '"]');*/
                        /*var strC = '<input type="text" placeholder="-" value="' + trStr[j] + '"/><span style="display: none;">' + trStr[j] + '</span>';
                          td.html(strC);*/
                        td.find("input").val(trStr[j]);
                        td.find("span").html(trStr[j]);
                    }
                }
                //防止onpaste事件起泡
                return false;
            }
        });
    }

    //生成模板
    function buildTables(m,checkdata) {
        var checkedNodes = checkdata;
        if (!m) {
            newtemp = true;
        }
        var xDatas = []; //X轴数据
        var yDatas = []; //Y轴数据

        if (x != "" && y != "") {
            if (x == "group") {
                // 指标是不能为空的，此时如果column_info为空则是错误的
                if (column_info === "") {
                    return;
                }
                var rootnode = [];
                for (var i = 0; i < checkedNodes.length; i++) {
                    var sub = checkedNodes[i].sub;
                    var root = checkedNodes[i];
                    root.sub = [];
                    var subdata = root;
                    if (sub.length > 0) {
                        for (var k = 0; k < sub.length; k++) {
                            if (sub[k].checked) {
                                for (var j = 0; j < row_info.length; j++) {
                                    sub[k].sub.push(row_info[j]);
                                }
                                subdata.sub.push(sub[k]);
                            }
                        }
                    } else {
                        for (var j = 0; j < row_info.length; j++) {
                            checkedNodes[i].sub.push(row_info[j]);
                        }
                    }
                    rootnode.push(subdata);
                }
                xDatas = rootnode;
            } else {
                // 指标是不能为空的，此时如果column_info为空则是错误的
                if (row_info === "") {
                    return;
                }
                var rootnode = [];
                for (var i = 0; i < checkedNodes.length; i++) {
                    var sub = checkedNodes[i].sub;
                    var root = checkedNodes[i];
                    root.sub = [];
                    var subdata = root;
                    if (sub.length > 0) {
                        for (var k = 0; k < sub.length; k++) {
                            if (sub[k].checked) {
                                for (var j = 0; j < column_info.length; j++) {
                                    sub[k].sub.push(column_info[j]);
                                }
                                subdata.sub.push(sub[k]);
                            }
                        }
                    } else {
                        for (var j = 0; j < column_info.length; j++) {
                            checkedNodes[i].sub.push(column_info[j]);
                        }
                    }
                    rootnode.push(subdata);
                }
                yDatas = rootnode;
            }
            if (xDatas.length == 0) {
                xDatas = row_info;
            }
            if (yDatas.length == 0) {
                yDatas = column_info;
            }
            x == "iden"
                ? main.createTable(xDatas, yDatas)
                : main.createTable(yDatas, xDatas);
            isCreate = true;
            bindValue();
            setTableStyle();
        }
    }

    function treeFilter(node) {
        return node.period_type == "dir" && node.checked;
    }

    //显示上传弹窗
    function showUploadDataModel() {
        let answer = filter.period && filter.curRegion.regioncode && rcid;
        if (!answer) {
            notice.msg('区划/区划配置/报告期 未选择完整，请选择完整！', {icon: 3});
            return;
        }
        let args = {
            cross_table:$("#left_div1").html(),
            column_table:$("#right_divx").html(),
            row_table:$("#left_div2").html(),
            data_table:$("#right_div2").html(),
            maid:curMeta.id,
            rcid:rcid,
            regioncode:filter.curRegion.regioncode
        }
        window.macroImportDataStatus = false;
        admin.open({
            title: '上传',
            url: 'importData/importData.html',
            data: args,     // 传递数据到表单页面
            end: function () {
                if(window.macroImportDataStatus){
                    admin.closeThisDialog();
                }
            },
            success: function (layero, dIndex) {
                // 弹窗超出范围不出现滚动条
                $(layero).children('.layui-layer-content').css('overflow', 'visible');
            }
        });
    }

    //收到录入数据
    function handleUploadData() {
        let answer = filter.period && filter.curRegion.regioncode && rcid;
        if (!answer) {
            notice.msg('区划/区划配置/报告期 未选择完整，请选择完整！', {icon: 3});
            return;
        }

        var datas = [];
        $("#data_table td").each(function() {
            var data = {
                period: $(this).attr("period"),
                idenComparsion: $(this).attr("idencomparsion"),
                idenCode: $(this).attr("idencode"),
                idenName: "",
                groupCode: $(this).attr("groupcode"),
                groupName: "",
                isRegionCode: $(this).attr("isregioncode"),
                value: $(this)
                    .find("input")
                    .val(),
                idenCodePath: $(this).attr("idenpath"),
                idenNamePath: $(this).attr("idenpathname"),
                groupCodePath: $(this).attr("grouppath"),
                groupNamePath: $(this).attr("grouppathname")
            };
            datas.push(data);
        });

        const args = {
            cross_table:$("#left_div1").html(),
            column_table:$("#right_divx").html(),
            row_table:$("#left_div2").html(),
            data_table:$("#right_div2").html(),
            macroDatas:JSON.stringify(datas),
            maid:curMeta.id,
            rcid:rcid,
            regioncode:filter.curRegion.regioncode,
        };

        axios({
            method: 'POST',
            url:macroUrl.data.addData,
            data:$.param(args),
            headers:{
                'Authorization':Authorization
            }
        })
            .then(function (response) {
                if(response.data.message=="success"){
                    layer.msg("操作成功", {icon: 1});
                    setTimeout(function () {
                        admin.closeThisDialog();
                    },1500)
                }else{
                    layer.msg(response.data.message, {icon: 2});
                }
            })
            .catch(function (error) {
                console.log(error);
                if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                    // 进行重新登陆
                    localStorage.clear();
                    top.location.href=loginPageUrl;
                }
            });

    }
});

